<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Attachment extends Model
{

    use SoftDeletes;

    protected $fillable = [
        'disk',
        'name',
        'url',
        'mime',
        'type',
        'size',
        'user_id',
        'sync_task_id',
        'sync_file_url',
        'sync_file_process',
        'sync_task_params'
    ];

    public function user()
    {
        return $this->belongsTo(User::class);
    }

    public function scopeImage($query)
    {
        return $query->where('type', 'image');
    }

    public function scopeVideo($query)
    {
        return $query->where('type', 'video');
    }

    public function scopeOther($query)
    {
        return $query->whereNotIn('type', 'video,image');
    }

    public function getUrlAttribute($attribute)
    {
        return attachment_out($attribute);
    }

    public function getSyncFileUrlAttribute($attribute)
    {
        return attachment_out($attribute);
    }

    protected function setUrlAttribute($attribute)
    {
        $this->attributes['url'] = attachment_in($attribute);
    }

    public function getSyncTaskParamsAttribute($attribute)
    {
        return json_decode($attribute, true);
    }

    protected function setSyncTaskParamsAttribute($attribute)
    {
        $this->attributes['sync_task_params'] = json_encode($attribute, JSON_FORCE_OBJECT);
    }
}
